package com.entor.springboot201912.controller;

import com.entor.springboot201912.model.Msg;
import com.entor.springboot201912.util.SmsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.HttpConstraintElement;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/send")
public class SendController {

    @RequestMapping("/sendPhoneCode/{phone}")
    @ResponseBody
    public Msg sendPhoneCode(@PathVariable(value="phone") String phone,
                             HttpSession session){
        Msg m = new Msg("发送失败",false,null);
        boolean b = SmsUtil.sendPhoneCode(phone,session);
        if(b){
            m.setIs(true);
            m.setMsg("短信验证码已发送，注意查收");
        }
        return m;
    }

    @RequestMapping("/phonePage")
    public String phonePage(){
        return "/sendPhoneCode";
    }

    @RequestMapping("/toReg")
    @ResponseBody
    public Msg toReg(String phone, String code, HttpSession session){
        Msg m = new Msg("验证失败",false,null);
        //从session中获取验证码
        String phoneCode =session.getAttribute("phoneCode")+"";
        if(phoneCode.equals(phone+"-"+code)){
            m.setMsg("验证成功");
            m.setIs(true);
        }
        session.removeAttribute("phoneCode");//移除验证码
        return m;
    }
}
